package es.xpt.ysf.view.remoting;

import java.io.IOException;

import javax.faces.FacesException;
import javax.faces.context.FacesContext;
import javax.faces.event.PhaseEvent;
import javax.faces.event.PhaseId;
import javax.faces.event.PhaseListener;


public class RemotingPhaseListener implements PhaseListener {
	
	private static final long serialVersionUID = 6982586257992618200L;
	

	/**
     * <p>Perform interception processing activities <em>after</em> the
     * specified phase processing has been performed.</p>
     *
     * @param event <code>PhaseEvent</code> to be processed
     */
    public void afterPhase(PhaseEvent event) {

        // Acquire a reference to the FacesContext for this request
        FacesContext context = event.getFacesContext();
        String resourceId = mapViewId(context);
        if (resourceId != null) {
            try {
            	MethodBindingProcessor processor = new MethodBindingProcessor();
                processor.process(context, resourceId);
            } catch (IOException e) {
                throw new FacesException(e);
            }
        }
    }


    /**
     * <p>Perform interception processing activities <em>before</em> the
     * specified phase processing has been performed.</p>
     *
     * @param event <code>PhaseEvent</code> to be processed
     */
    public void beforePhase(PhaseEvent event) {

        // No processing required

    }


    /**
     * <p>Return the identifier of the JavaServer Faces request processing
     * lifecycle phase(s) that we are interested in.</p>
     */
    public PhaseId getPhaseId() {

        return PhaseId.RESTORE_VIEW;

    }
    
    private String mapViewId(FacesContext context) {

        // Extract the view identifier we will be using to match against
        String viewId = viewId(context);
        if (viewId == null) {
            return null;
        }

        // Perform prefix or extension matching as requested
        if (viewId.startsWith("/dynamic") && !viewId.equals("/dynamic")) {
            return viewId.substring(8);
        } else {
            return null;
        }
    

    }
    
    /**
     * <p>Extract and return the view identifier for this request, after
     * stripping any replacement suffix if <code>FacesServlet</code> is
     * being extension mapped.</p>
     *
     * @param context <code>FacesContext</code> for the current request
     */
    private String viewId(FacesContext context) {

        // Get the raw view identifier
        String viewId = context.getViewRoot().getViewId();

        // If the view identifier ends with the configured (or default)
        // replacement suffix, just strip it and return
        String extension = ".jspx";
        if (viewId.endsWith(extension)) {
            return viewId.substring(0, viewId.length() - extension.length());
        }
        extension = ".xhtml";
        if (viewId.endsWith(extension)) {
            return viewId.substring(0, viewId.length() - extension.length());
        }

        return viewId;

    }



}
